package com.apimanage.user.controller;

import com.apimanage.common.dto.UserDTO;
import com.apimanage.common.entity.Result;
import com.apimanage.common.service.UserCenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户控制器
 *
 * @author API管理平台
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserCenterService userService;

    /**
     * 获取当前用户信息
     */
    @GetMapping("/info")
    public Result<?> getUserInfo(@RequestHeader("X-User-Id") Long userId) {
        UserDTO userDTO = userService.getUserById(userId);
        if (userDTO == null) {
            return Result.error("用户不存在");
        }
        return Result.success("获取成功", userDTO);
    }

    /**
     * 更新用户信息
     */
    @PutMapping("/update")
    public Result<?> updateUser(@RequestBody UserDTO userDTO) {
        boolean updated = userService.updateUser(userDTO);
        if (updated) {
            return Result.success("更新成功");
        } else {
            return Result.error("更新失败");
        }
    }

    /**
     * 修改密码
     */
    @PostMapping("/change-password")
    public Result<?> changePassword(@RequestBody Map<String, String> passwordParams) {
        Long userId = Long.parseLong(passwordParams.get("userId"));
        String oldPassword = passwordParams.get("oldPassword");
        String newPassword = passwordParams.get("newPassword");

        boolean changed = userService.changePassword(userId, oldPassword, newPassword);
        if (changed) {
            return Result.success("密码修改成功");
        } else {
            return Result.error("密码修改失败");
        }
    }

    /**
     * 重置密码（管理员操作）
     */
    @PostMapping("/reset-password")
    public Result<?> resetPassword(@RequestBody Map<String, String> passwordParams) {
        Long userId = Long.parseLong(passwordParams.get("userId"));
        String newPassword = passwordParams.get("newPassword");

        boolean reset = userService.resetPassword(userId, newPassword);
        if (reset) {
            return Result.success("密码重置成功");
        } else {
            return Result.error("密码重置失败");
        }
    }

    /**
     * 更新用户状态
     */
    @PostMapping("/update-status")
    public Result<?> updateUserStatus(@RequestBody Map<String, Object> statusParams) {
        Long userId = Long.parseLong(statusParams.get("userId").toString());
        Integer status = Integer.parseInt(statusParams.get("status").toString());

        boolean updated = userService.updateUserStatus(userId, status);
        if (updated) {
            return Result.success("状态更新成功");
        } else {
            return Result.error("状态更新失败");
        }
    }

    /**
     * 获取用户列表（分页）
     * 需要管理员角色才能访问
     */
    @GetMapping("/list")
    public Result<?> getUserList(@RequestParam(defaultValue = "1") Integer page, 
                               @RequestParam(defaultValue = "10") Integer pageSize) {
        List<UserDTO> userList = userService.getUserList(page, pageSize);
        Integer total = userService.countUsers();

        Map<String, Object> result = new HashMap<>();
        result.put("list", userList);
        result.put("total", total);
        result.put("page", page);
        result.put("pageSize", pageSize);

        return Result.success("获取成功", result);
    }
}